package com.baidu.ueditor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PathFormat {
	public static final String TIME = "time";
	public static final String FULL_YEAR = "yyyy";
	public static final String YEAR = "yy";
	public static final String MONTH = "mm";
	public static final String DAY = "dd";
	public static final String HOUR = "hh";
	public static final String MINUTE = "ii";
	public static final String SECOND = "ss";
	public static final String RAND = "rand";
	private static Date currentDate = null;
	static final String REGEX = "\\{([^\\}]+)\\}" ;

	public static String parse(String input) {
		Pattern pattern = Pattern.compile(REGEX, 2);
		Matcher matcher = pattern.matcher(input);

		currentDate = new Date();

		StringBuffer sb = new StringBuffer();

		while (matcher.find()) {
			matcher.appendReplacement(sb, getString(matcher.group(1)));
		}

		matcher.appendTail(sb);

		return sb.toString();
	}

	public static String format(String input) {
		return input.replace("\\", "/");
	}

	public static String parse(String input, String filename) {
		Pattern pattern = Pattern.compile(REGEX, 2);
		Matcher matcher = pattern.matcher(input);
		String matchStr = null;

		currentDate = new Date();

		StringBuffer sb = new StringBuffer();

		while (matcher.find()) {
			matchStr = matcher.group(1);
			if (matchStr.indexOf("filename") != -1) {
				filename = filename.replace("$", "\\$").replaceAll("[\\/:*?\"<>|]", "");
				matcher.appendReplacement(sb, filename);
			} else {
				matcher.appendReplacement(sb, getString(matchStr));
			}

		}

		matcher.appendTail(sb);

		return sb.toString();
	}

	private static String getString(String pattern) {
		pattern = pattern.toLowerCase();

		if (pattern.indexOf(TIME) != -1)
			return getTimestamp();
		if (pattern.indexOf(FULL_YEAR) != -1)
			return getFullYear();
		if (pattern.indexOf(YEAR) != -1)
			return getYear();
		if (pattern.indexOf(MONTH) != -1)
			return getMonth();
		if (pattern.indexOf(DAY) != -1)
			return getDay();
		if (pattern.indexOf(HOUR) != -1)
			return getHour();
		if (pattern.indexOf(MINUTE) != -1)
			return getMinute();
		if (pattern.indexOf(SECOND) != -1)
			return getSecond();
		if (pattern.indexOf(RAND) != -1) {
			return getRandom(pattern);
		}

		return pattern;
	}

	private static String getTimestamp() {
		return String.valueOf(System.currentTimeMillis());
	}

	private static String getFullYear() {
		return new SimpleDateFormat(FULL_YEAR).format(currentDate);
	}

	private static String getYear() {
		return new SimpleDateFormat(YEAR).format(currentDate);
	}

	private static String getMonth() {
		return new SimpleDateFormat("MM").format(currentDate);
	}

	private static String getDay() {
		return new SimpleDateFormat(DAY).format(currentDate);
	}

	private static String getHour() {
		return new SimpleDateFormat(HOUR).format(currentDate);
	}

	private static String getMinute() {
		return new SimpleDateFormat(MINUTE).format(currentDate);
	}

	private static String getSecond() {
		return new SimpleDateFormat(SECOND).format(currentDate);
	}

	private static String getRandom(String pattern) {
		int length = 0;
		pattern = pattern.split(":")[1].trim();

		length = Integer.parseInt(pattern);

		return String.valueOf(Math.random()).replace(".", "").substring(0, length);
	}

}
